package com.example.fish.controller;

import com.example.fish.pojo.Event;
import com.example.fish.pojo.User;
import com.example.fish.service.EventService;
import com.example.fish.utils.JsonResult;
import com.example.fish.vo.EventDetail;
import com.example.fish.vo.EventOther;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/event")
public class EventController extends BaseController{

    @Autowired
    private EventService eventService;

    @PostMapping("/getAllPage")
    private JsonResult<PageInfo<EventDetail>> getAll(Integer pageNum){
        PageInfo<EventDetail> page = eventService.getAll(pageNum);
        return new JsonResult<>(200,"success",page);
    }
//    分类查询
    @PostMapping("/notStart")
    private JsonResult<PageInfo<EventDetail>> byStart(Integer pageNum){
        PageInfo<EventDetail> notStart = eventService.getNotStart(pageNum);
        return new JsonResult<>(200,"success",notStart);
    }
//    状态查询
    @PostMapping("/state")
    private JsonResult<PageInfo<EventDetail>> byState(Integer pageNum,String state){
        PageInfo<EventDetail> byState = eventService.findByState(pageNum, state);
        return new JsonResult<>(200,"success",byState);
    }
//    查询other 我发起的/参与的
    @PostMapping("/launch")
    private JsonResult<PageInfo<EventOther>> launch(Integer pageNum, String flag, HttpSession session){
        User user =(User) session.getAttribute("user");
        PageInfo<EventOther> byUserId = eventService.findByUserId(pageNum, user.getUserid(), flag);
        return new JsonResult<>(200,"success",byUserId);
    }
//    删除自己举办的event
    @PostMapping("/delete/own")
    private JsonResult<Void> deleteOwn(Integer eventid){
        eventService.deleteByEventId(eventid);
        return new JsonResult<>(200,"success",null);
    }

//    报名活动
    @PostMapping("/sign")
    private JsonResult<Void> sign(Integer eventid, HttpSession session){
        User user =(User) session.getAttribute("user");
        eventService.signIn(user.getUserid(),eventid);
        return new JsonResult<>(200,"success",null);
    }
    @PostMapping("/isSigned")
    private JsonResult<Boolean> isSigned(Integer eventid,HttpSession session){
        User user =(User) session.getAttribute("user");
        Boolean aBoolean = eventService.IsSigned(user.getUserid(), eventid);
        return new JsonResult<>(200,"success",aBoolean);
    }

    @PostMapping("/cancel/sign")
    private JsonResult<Void> cancelSign(Integer eventid,HttpSession session){
        User user =(User) session.getAttribute("user");
        eventService.signOut(user.getUserid(), eventid);
        return new JsonResult<>(200,"success",null);
    }
    @PostMapping("/cancelEvent")
    private JsonResult<Void> cancelEvent(Integer eventid){
        eventService.cancelEvent(eventid);
        return new JsonResult<>(200,"success",null);
    }
    @PostMapping("/getByid")
    private JsonResult<Event> getbyid(Integer eventid){
        Event byId = eventService.getById(eventid);
        return new JsonResult<>(200,"success",byId);
    }

    @PostMapping("/hold")
    private JsonResult<Void> hold(HttpSession session,@RequestBody Event event){
        User user =(User) session.getAttribute("user");
        eventService.holdEvent(user.getUserid(), event);
        return new JsonResult<>(200,"success",null);
    }
}
